package com.microblogforsim.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.lzy.okgo.OkGo;
import com.microblogforsim.R;
import com.microblogforsim.adapter.HotWordAdapter;
import com.microblogforsim.callback.JsonCallback;
import com.microblogforsim.model.HotWord;
import com.microblogforsim.model.LzyResponse;
import com.microblogforsim.model.Topic;
import com.microblogforsim.model.User;
import com.microblogforsim.utils.ACache;
import com.microblogforsim.utils.Urls;

import java.util.ArrayList;
import java.util.List;

public class Fragment_Find extends Fragment implements View.OnClickListener {
    private View view;
    //    热词适配器
    private HotWordAdapter adapter;
    //    热词数据源
    private ArrayList<HotWord> data;
    //    热词列表
    private RecyclerView rl_list;
    //    取消按钮
    private TextView btn_cancle;
    private List<Topic.DataBean> topic;
    private ACache mCache;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_find, container, false);
        mCache = ACache.get(getContext());
        init();
        get_hot_topic();

        return view;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
//            更新数据
        }
    }

    /**
     * 获取用户信息
     */
    private void get_hot_topic() {
        OkGo.<LzyResponse<Topic>>get(Urls.SERVER + "Topic/getAll")//
                .tag(this)//
                .headers("header1", "headerValue1")//
                .execute(new JsonCallback<LzyResponse<Topic>>() {
                    @Override
                    public void onSuccess(com.lzy.okgo.model.Response<LzyResponse<Topic>> response) {
                        // handleResponse(response);
                        Log.d("TAG", "onSuccess: " + response.body().data.getData().get(5).getTopic() + "");
                        if (response.body().msg.equals("success")) {

                            topic = response.body().data.getData();
                            mCache.put("hot_topic",response.body().data);
                            setList();
                        } else {
                            Toast.makeText(getContext(), "网络请求失败", Toast.LENGTH_SHORT).show();
                        }

                    }

                    @Override
                    public void onError(com.lzy.okgo.model.Response<LzyResponse<Topic>> response) {
                        // handleError(response);
                        Log.d("TAG", "err: " + response.body().toString());
                        Toast.makeText(getContext(), "网络出错", Toast.LENGTH_SHORT).show();
                    }
                });

    }

    private void setList() {

        data = new ArrayList<>();
        for (int i = 0; i < 5; i++) {

            data.add(new HotWord(topic.get(i).getTopic() + "", 1));
        }
        data.add(new HotWord(topic.get(5).getTopic() + "", 2));
        data.add(new HotWord(topic.get(6).getTopic() + "", 3));
        data.add(new HotWord("更多热搜", 0));
        adapter = new HotWordAdapter();
        adapter.replaceAll(data);
        rl_list.setAdapter(adapter);

        adapter.setOnRecyItemClickListener(new HotWordAdapter.OnRecyItemClickListener() {
            @Override
            public void onClick(View view, int position) {
                switch (position) {
                    default:
                        Toast.makeText(getContext(), position + "", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void init() {
        btn_cancle = view.findViewById(R.id.fragment_find_cancle);
        rl_list = view.findViewById(R.id.fragment_find_list);
        rl_list.setHasFixedSize(true);
        rl_list.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        btn_cancle.setOnClickListener(this);
        Topic list = (Topic)mCache.getAsObject("hot_topic");
        if(list!=null&&list.getData().size()>0){
            topic = list.getData();
            setList();
       }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.fragment_find_cancle:
                Toast.makeText(getContext(), "取消按钮", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}
